package com.ohuang.wallpapermanager.config

import android.annotation.SuppressLint
import com.ohuang.wallpapermanager.storage.storage.SpStorageUtil
import com.ohuang.wallpapermanager.storage.storage.getValue
import com.ohuang.wallpapermanager.storage.storage.setValue
import com.ohuang.wallpapermanager.util.AppUtil
import com.ohuang.wallpapermanager.util.TimeUtil

@SuppressLint("StaticFieldLeak")
object LogConfig {

    private var storageUtil = SpStorageUtil(AppUtil.getInstance().application, "LogConfig")

    private var lastClearLogTime:Long by storageUtil.get("lastClearLogTime", 0L)

    private var logs:String by storageUtil.get("logs", "")

    private var backupsLogs:String by storageUtil.get("backupsLogs", "")

    fun appLog(log:String) {
        shouldClearLog()
        logs += "${TimeUtil.getSimpleTime(System.currentTimeMillis())}:"+log + "\n"
    }

    fun getLog():String {
        return backupsLogs+"\n"+logs
    }

    fun clearAllLog() {
        lastClearLogTime=System.currentTimeMillis()
        backupsLogs= ""
        logs = ""
    }

    private fun clearLog() {
        lastClearLogTime=System.currentTimeMillis()
        backupsLogs= logs
        logs = ""
    }

     private fun shouldClearLog() {
         if (lastClearLogTime == 0L) {
             lastClearLogTime = System.currentTimeMillis()
             return
         }
         if (System.currentTimeMillis() - lastClearLogTime > 12*3600_000L){
             clearLog()
         }
     }


}